<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/12
 * Time: 16:30
 */

namespace app\api\job;


use app\common\model\WalletRecord;
use think\Db;
use think\queue\Job;

class HandlePay
{
    public $delay = 300;
    public function fire(Job $job,$data)
    {
        //创建支付订单，待支付，5分钟延迟后再检测订单是否被支付如果被支付。删除队列
        $record = WalletRecord::where('record_sn',$data['record_sn'])->find();
        if( $record && $record['pay_status'] == 1 ){ //如果存在则，检测该订单记录是否支付，已经支付，删除任务
            if( $record['pay_status'] == 1 || $record['pay_status'] == -1 ){
                $job->delete();
            }
        }elseif( $record && $record['pay_status'] == 0 ){//未支付
            if( time() -$record['createtime'] >= $this->delay ){
                Db::name('wallet_record')->where('record_sn',$data['record_sn'])->update([
                    'pay_status'    => -1,
                    'pay_time'      => time(),
                ]);
                $job->delete();
            }
        }else{
            if( $job->attempts() > 3 ){ //重试3次以上删除任务
                $job->delete();
            }
        }
        unset($data);
    }
}